#include "serialportmanager.h"
#include <QSettings>
#include <QTime>
#include <QDebug>
#include <qcoreapplication.h>
#include <QTimer>
SerialPortManager::SerialPortManager()
{
    m_comLight = new QSerialPort();
    m_comHandler = new QSerialPort();
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "LightController");
	int strobe = app.value("strobe").toInt();
	m_bIsStrobe = strobe == 1;
    OpenComLight();
    OpenComHandler();
}

void SerialPortManager::InitController()
{
	if (m_bIsStrobe)
	{
		QObject::connect(m_comLight, &QSerialPort::readyRead, this, [=]() {
			QByteArray bytes;
			bytes = m_comLight->readAll();
			qDebug() << "read AB:" << bytes;
			m_comLight->clear();
			for (int i = 0; i < bytes.length(); i++)
			{

			}
		});
		SetExternalTrigger(true);
		QTimer::singleShot(200, [=]() {
			int delays[3] = { 999, 999, 999 };
			SetAllDelay(delays, 3);
		});
		
	}
}

void SerialPortManager::WaitForWriteAndClose()
{
	//return;
	//int k = 0;
	//while (true)
	//{
	//	qint64 btw = m_comLight->bytesToWrite();		
	//	if (btw == 0 || k >= 10)
	//	{
	//		StopComLight();
	//		break;
	//	}
	//	else {
	//		DelayMs(10);
	//		k++;
	//	}
	//}
	/*QTime t;
	t.start();
	while (t.elapsed() < 50)
	{
		QCoreApplication::processEvents();
	}*/
	extern void DelayMs(int ms);
	DelayMs(100);
}

void SerialPortManager::OpenComLight()
{
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "Com");
    QString port = app.value("PortNameLight").toString();
    int baud = app.value("BaudRateLight").toString().toInt();
    int data = app.value("DataBitsLight").toString().toInt();
    int stop = app.value("StopBitsLight").toString().toInt();
    QString parity = app.value("ParityLight").toString();
	if (m_comLight->isOpen())
	{
		m_comLight->close();
	}
    
    if (port.startsWith("COM", Qt::CaseInsensitive))
    {
        m_comLight->setPortName(port);
    }

    bool b = m_comLight->open(QIODevice::ReadWrite);
    //设置波特率
    //设置数据位数
    switch (baud) {
    case 9600:
        m_comLight->setBaudRate(QSerialPort::Baud9600);
        break;
    case 19200:
        m_comLight->setBaudRate(QSerialPort::Baud19200);
        break;
    case 38400:
        m_comLight->setBaudRate(QSerialPort::Baud38400);
        break;
    case 57600:
        m_comLight->setBaudRate(QSerialPort::Baud57600);
        break;
    default:
        m_comLight->setBaudRate(QSerialPort::Baud115200);
        break;
    }
    switch (data)
    {
    case 8:
        m_comLight->setDataBits(QSerialPort::Data8);//设置数据位8
        break;
    case 7:
        m_comLight->setDataBits(QSerialPort::Data7);//设置数据位7
        break;
    default:
        m_comLight->setDataBits(QSerialPort::Data8);//设置数据位8
        break;
    }
    //设置校验位
    if (parity.compare("Even", Qt::CaseInsensitive) == 0)
    {
        m_comLight->setParity(QSerialPort::EvenParity);
    }
    else if (parity.compare("Odd", Qt::CaseInsensitive) == 0)
    {
        m_comLight->setParity(QSerialPort::OddParity);
    }
    else {
        m_comLight->setParity(QSerialPort::NoParity);
    }

    //设置停止位
    switch (stop)
    {
    case 2:
        m_comLight->setStopBits(QSerialPort::TwoStop);
        break;
    default:
        m_comLight->setStopBits(QSerialPort::OneStop);//停止位设置为1
        break;
    }
    //设置流控制
    m_comLight->setFlowControl(QSerialPort::NoFlowControl);//设置为无流控制
	qDebug() << "open com light" << port << baud << b;
}

void SerialPortManager::OpenComHandler()
{
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "Com");
    QString port = app.value("PortName").toString();
    int baud = app.value("BaudRate").toString().toInt();
    int data = app.value("DataBits").toString().toInt();
    int stop = app.value("StopBits").toString().toInt();
    QString parity = app.value("Parity").toString();

    if(m_comHandler->isOpen())
    {
        m_comHandler->close();
    }
    if(port.startsWith("COM", Qt::CaseInsensitive))
    {
        m_comHandler->setPortName(port);
    }

	bool b = m_comHandler->open(QIODevice::ReadWrite);
    //设置波特率
    //设置数据位数
    switch (baud) {
    case 9600:
        m_comHandler->setBaudRate(QSerialPort::Baud9600);
        break;
    case 19200:
        m_comHandler->setBaudRate(QSerialPort::Baud19200);
        break;
    case 38400:
        m_comHandler->setBaudRate(QSerialPort::Baud38400);
        break;
    case 57600:
        m_comHandler->setBaudRate(QSerialPort::Baud57600);
        break;
    default:
        m_comHandler->setBaudRate(QSerialPort::Baud115200);
        break;
    }
    switch (data)
    {
    case 8:
        m_comHandler->setDataBits(QSerialPort::Data8);//设置数据位8
        break;
    case 7:
        m_comHandler->setDataBits(QSerialPort::Data7);//设置数据位7
        break;
    default:
            m_comHandler->setDataBits(QSerialPort::Data8);//设置数据位8
        break;
    }
    //设置校验位
    if(parity.compare("Even", Qt::CaseInsensitive) == 0)
    {
        m_comHandler->setParity(QSerialPort::EvenParity);
    }
    else if(parity.compare("Odd", Qt::CaseInsensitive) == 0)
    {
        m_comHandler->setParity(QSerialPort::OddParity);
    }
    else {
        m_comHandler->setParity(QSerialPort::NoParity);
    }

    //设置停止位
    switch (stop)
    {
    case 2:
        m_comHandler->setStopBits(QSerialPort::TwoStop);
        break;
    default:
        m_comHandler->setStopBits(QSerialPort::OneStop);//停止位设置为1
        break;
    }
    //设置流控制
    m_comHandler->setFlowControl(QSerialPort::NoFlowControl);//设置为无流控制
	qDebug() << "open com handler" << port << baud << b;
}

void SerialPortManager::StopComLight()
{
    if (m_comLight->isOpen())
    {
        m_comLight->close();
    }
}

void SerialPortManager::StopComHandler()
{
    if (m_comHandler->isOpen())
    {
        m_comHandler->close();
    }
}

void SerialPortManager::SendBuffer(char *resBuf, uint8_t length)
{
	if (!m_comHandler->isOpen())
	{
		OpenComHandler();
	}

	if (m_comHandler && m_comHandler->isOpen())
	{
		m_comHandler->write(resBuf, length);
		qDebug() << "send result via COM:" << resBuf;
	}
	else {
		qDebug() << "handler com error";
	}
}

void SerialPortManager::SendResult(bool bSite0, uint8_t bResult)
{
    if(!m_comHandler->isOpen())
    {
        OpenComHandler();
    }

    if (m_comHandler && m_comHandler->isOpen())
    {
        char resBuf[10] = { 0 };
        resBuf[0] = 0x8;
        resBuf[2] = 's';
        resBuf[3] = 'i';
        resBuf[4] = 't';
        resBuf[5] = 'e';
        resBuf[6] = bSite0 ? '0' : '1';
        resBuf[7] = '=';
        resBuf[1] = resBuf[8] =   '0'+ bResult;
        resBuf[9] = (resBuf[1] + resBuf[2] + resBuf[3] + resBuf[4] + resBuf[5] + resBuf[6] + resBuf[7] + resBuf[8]) & 0xff;
        m_comHandler->write(resBuf, 10);
        qDebug() << "send result via COM:" << resBuf;
    }
    else{
        qDebug() << "handler com error";
    }
}

void SerialPortManager::SendVersion()
{
	if (!m_comHandler->isOpen())
	{
		OpenComHandler();
	}

	if (m_comHandler && m_comHandler->isOpen())
	{
		QString v = QCoreApplication::applicationVersion();
		QByteArray  ch1 = v.toUtf8();
		QByteArray  ch2 = "@V";
		ch2 += ch1;
		ch2.push_back('+');
		m_comHandler->write(ch2.data(), ch2.length());
	
		qDebug() << "send version via COM:" << ch2;
	}
	else {
		qDebug() << "handler com error";
	}
}

void SerialPortManager::SendNextPos(int x, int y)
{
	if (!m_comHandler->isOpen())
	{
		OpenComHandler();
	}

	if (m_comHandler && m_comHandler->isOpen())
	{
		QString v = QString("%1,%2").arg(x).arg(y);
		QByteArray  ch1 = v.toUtf8();
		QByteArray  ch2 = "10NEXTPOS";
		ch2 += ch1;
		uint8_t crc = 0;
		for each (char var in ch2)
		{
			crc += var;
		}
		ch2.push_front(ch2.length());
		ch2.push_back(crc);
		m_comHandler->write(ch2.data(), ch2.length());

		qDebug() << "send next pos via COM:" << ch2;
	}
	else {
		qDebug() << "handler com error";
	}
}

void SerialPortManager::SetLightBrightnessA(uint8_t light)
{
	//qDebug() << __FUNCTION__ << light;
    if(!m_comLight->isOpen())
    {
        OpenComLight();
    }
    if (m_comLight && m_comLight->isOpen())
    {
			char resBuf[7] = { 0 };
			resBuf[0] = 'S';
			resBuf[1] = 'A';
			resBuf[2] = '0';
			resBuf[3] = '0' + (light / 100);
			resBuf[4] = '0' + (light % 100) / 10;
			resBuf[5] = '0' + (light % 10);
			resBuf[6] = '#';
			m_comLight->write(resBuf, 7);
		WaitForWriteAndClose();       
    }
    else{
        qDebug() << "light com error";
    }
}

void SerialPortManager::SetLightBrightnessB(uint8_t light)
{
	//qDebug() << __FUNCTION__ << light;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[7] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'B';
		resBuf[2] = '0';
		resBuf[3] = '0' + (light / 100);
		resBuf[4] = '0' + (light % 100) / 10;
		resBuf[5] = '0' + (light % 10);
		resBuf[6] = '#';
		//qDebug() << "isWritable" << m_comLight->isWritable();
		m_comLight->write(resBuf, 7);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetLightBrightnessC(uint8_t light)
{
	//qDebug() << __FUNCTION__ << light;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[7] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'C';
		resBuf[2] = '0';
		resBuf[3] = '0' + (light / 100);
		resBuf[4] = '0' + (light % 100) / 10;
		resBuf[5] = '0' + (light % 10);
		resBuf[6] = '#';
		//qDebug() << "isWritable" << m_comLight->isWritable();
		m_comLight->write(resBuf, 7);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetLightBrightnessD(uint8_t light)
{
	//qDebug() << __FUNCTION__ << light;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[7] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'D';
		resBuf[2] = '0';
		resBuf[3] = '0' + (light / 100);
		resBuf[4] = '0' + (light % 100) / 10;
		resBuf[5] = '0' + (light % 10);
		resBuf[6] = '#';
		//qDebug() << "isWritable" << m_comLight->isWritable();
		m_comLight->write(resBuf, 7);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}
//void SerialPortManager::SetWhiteLightBrightness(uint8_t light)
//{
//	qDebug() << __FUNCTION__ << light;
//    if(!m_comLight->isOpen())
//    {
//        OpenComLight();
//    }
//    if (m_comLight && m_comLight->isOpen())
//    {
//        char resBuf[7] = { 0 };
//        resBuf[0] = 'S';
//        resBuf[1] = 'C';
//        resBuf[2] = '0';
//        resBuf[3] = '0' + (light / 100);
//        resBuf[4] = '0' + (light % 100) / 10;
//        resBuf[5] = '0' + (light % 10);
//        resBuf[6] = '#';
//		//qDebug() << "isWritable" << m_comLight->isWritable();
//        m_comLight->write(resBuf, 7);
//		//qDebug() << "write" << m_comLight->errorString();
//    }
//    else{
//        qDebug() << "light com error";
//    }
//}

//void SerialPortManager::TurnOffRedLight()
//{
//    SetRedLightBrightness(0);
//}
//
//void SerialPortManager::TurnOffWhiteLight()
//{
//    SetBlueLightBrightness(0);
//}

void SerialPortManager::GetLightBrightnessA()
{
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[3] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'A';
		resBuf[2] = '#';
		m_comLight->write(resBuf, 3);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::GetLightBrightnessB()
{
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[3] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'B';
		resBuf[2] = '#';
		m_comLight->write(resBuf, 3);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::GetLightBrightnessC()
{
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[3] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'C';
		resBuf[2] = '#';
		m_comLight->write(resBuf, 3);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::GetLightBrightnessD()
{
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[3] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'D';
		resBuf[2] = '#';
		m_comLight->write(resBuf, 3);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}
void SerialPortManager::SetTwoLightBrightnessSync(uint8_t red, uint8_t blue)
{
	qDebug() << __FUNCTION__ << red << blue;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}

	//QObject::connect(m_comLight, &QSerialPort::readyRead, this, [=]() {
	//	QByteArray bytes;
	//	bytes = m_comLight->readAll();
	//	//qDebug() << "read AB:" << bytes;
	//	m_comLight->clear();
	//	for (int i = 0; i < bytes.length(); i++)
	//	{
	//		if (bytes[i] == 'B' || bytes[i] == 'A')
	//		{
	//			//bytes.remove(i, 1);
	//			disconnect(m_comLight, &QSerialPort::readyRead, 0, 0);
	//			emit setLightEvent();
	//			break;
	//		}
	//	}
	//});

	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[14] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'A';
		resBuf[2] = '0';
		resBuf[3] = '0' + (red / 100);
		resBuf[4] = '0' + (red % 100) / 10;
		resBuf[5] = '0' + (red % 10);
		resBuf[6] = '#';
		resBuf[7 + 0] = 'S';
		resBuf[7 + 1] = 'B';
		resBuf[7 + 2] = '0';
		resBuf[7 + 3] = '0' + (blue / 100);
		resBuf[7 + 4] = '0' + (blue % 100) / 10;
		resBuf[7 + 5] = '0' + (blue % 10);
		resBuf[7 + 6] = '#';
		m_comLight->write(resBuf, 14);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetFourLightBrightnessSync(uint8_t red, uint8_t blue, uint8_t white, uint8_t white2)
{
	qDebug() << __FUNCTION__ << red << blue << white << white2;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}

	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[28] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'A';
		resBuf[2] = '0';
		resBuf[3] = '0' + (red / 100);
		resBuf[4] = '0' + (red % 100) / 10;
		resBuf[5] = '0' + (red % 10);
		resBuf[6] = '#';
		resBuf[7 + 0] = 'S';
		resBuf[7 + 1] = 'B';
		resBuf[7 + 2] = '0';
		resBuf[7 + 3] = '0' + (blue / 100);
		resBuf[7 + 4] = '0' + (blue % 100) / 10;
		resBuf[7 + 5] = '0' + (blue % 10);
		resBuf[7 + 6] = '#';
		resBuf[7 * 2 + 0] = 'S';
		resBuf[7 * 2 + 1] = 'C';
		resBuf[7 * 2 + 2] = '0';
		resBuf[7 * 2 + 3] = '0' + (white / 100);
		resBuf[7 * 2 + 4] = '0' + (white % 100) / 10;
		resBuf[7 * 2 + 5] = '0' + (white % 10);
		resBuf[7 * 2 + 6] = '#';
		resBuf[7 * 3 + 0] = 'S';
		resBuf[7 * 3 + 1] = 'D';
		resBuf[7 * 3 + 2] = '0';
		resBuf[7 * 3 + 3] = '0' + (white2 / 100);
		resBuf[7 * 3 + 4] = '0' + (white2 % 100) / 10;
		resBuf[7 * 3 + 5] = '0' + (white2 % 10);
		resBuf[7 * 3 + 6] = '#';
		m_comLight->write(resBuf, 28);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetThreeLightBrightnessSync(uint8_t red, uint8_t blue, uint8_t white)
{
	qDebug() << __FUNCTION__ << red << blue << white;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}

	//QObject::connect(m_comLight, &QSerialPort::readyRead, this, [=]() {
	//	QByteArray bytes;
	//	bytes = m_comLight->readAll();
	////	qDebug() << "read ABC:" << bytes;
	//	m_comLight->clear();
	//	for (int i = 0; i < bytes.length(); i++)
	//	{
	//		if (bytes[i] == 'B' || bytes[i] == 'A' || bytes[i] == 'C')
	//		{
	//			//bytes.remove(i, 1);
	//			disconnect(m_comLight, &QSerialPort::readyRead, 0, 0);
	//			emit setLightEvent();
	//			break;
	//		}
	//	}
	//});

	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[21] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'A';
		resBuf[2] = '0';
		resBuf[3] = '0' + (red / 100);
		resBuf[4] = '0' + (red % 100) / 10;
		resBuf[5] = '0' + (red % 10);
		resBuf[6] = '#';
		resBuf[7 + 0] = 'S';
		resBuf[7 + 1] = 'B';
		resBuf[7 + 2] = '0';
		resBuf[7 + 3] = '0' + (blue / 100);
		resBuf[7 + 4] = '0' + (blue % 100) / 10;
		resBuf[7 + 5] = '0' + (blue % 10);
		resBuf[7 + 6] = '#';
		resBuf[7 * 2 + 0] = 'S';
		resBuf[7 * 2 + 1] = 'C';
		resBuf[7 * 2 + 2] = '0';
		resBuf[7 * 2 + 3] = '0' + (white / 100);
		resBuf[7 * 2 + 4] = '0' + (white % 100) / 10;
		resBuf[7 * 2 + 5] = '0' + (white % 10);
		resBuf[7 * 2 + 6] = '#';
		m_comLight->write(resBuf, 21);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetExternalTrigger(bool b)
{
	//qDebug() << __FUNCTION__ << light;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char resBuf[4] = { 0 };
		resBuf[0] = 'T';
		resBuf[1] = 'R';
		resBuf[2] = b ?'0':'1';
		resBuf[3] = '#';

		m_comLight->write(resBuf, 4);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetLightDelay(int ch, int delay)
{
	//qDebug() << __FUNCTION__ << light;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		const int LEN = 8;
		char resBuf[LEN] = { 0 };
		resBuf[0] = 'D';
		resBuf[1] = 'L';
		resBuf[2] = 'A' + ch;
		resBuf[3] = '0';
		resBuf[4] = '0' + (delay / 100);
		resBuf[5] = '0' + (delay % 100) / 10;
		resBuf[6] = '0' + (delay % 10);
		resBuf[7] = '#';

		m_comLight->write(resBuf, LEN);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetAllDelay(int delays[], int count)
{
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		char *resBuf = new char[8 * count];
		for (int i=0; i<count; i++)
		{
			resBuf[0 + 8 * i] = 'D';
			resBuf[1 + 8 * i] = 'L';
			resBuf[2 + 8 * i] = 'A' + i;
			resBuf[3 + 8 * i] = '0';
			resBuf[4 + 8 * i] = '0' + (delays[i] / 100);
			resBuf[5 + 8 * i] = '0' + (delays[i] % 100) / 10;
			resBuf[6 + 8 * i] = '0' + (delays[i] % 10);
			resBuf[7 + 8 * i] = '#';
		}
		
		m_comLight->write(resBuf, 8 * count);
		delete resBuf;
		WaitForWriteAndClose();
	}
}

void SerialPortManager::SetLightWidth(int ch, int width)
{
	//qDebug() << __FUNCTION__ << light;
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		const int LEN = 8;
		char resBuf[LEN] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'P';
		resBuf[2] = 'A' + ch;
		resBuf[3] = '0';
		resBuf[4] = '0' + (width / 100);
		resBuf[5] = '0' + (width % 100) / 10;
		resBuf[6] = '0' + (width % 10);
		resBuf[7] = '#';

		m_comLight->write(resBuf, LEN);
		WaitForWriteAndClose();
	}
	else {
		qDebug() << "light com error";
	}
}

void SerialPortManager::SetAllWidth(int width1, int width2, int width3)
{
	if (!m_comLight->isOpen())
	{
		OpenComLight();
	}
	if (m_comLight && m_comLight->isOpen())
	{
		const int LEN = 18;
		char resBuf[LEN] = { 0 };
		resBuf[0] = 'S';
		resBuf[1] = 'P';

		resBuf[2] = 'A';
		resBuf[3] = '0';
		resBuf[4] = '0' + (width1 / 100);
		resBuf[5] = '0' + (width1 % 100) / 10;
		resBuf[6] = '0' + (width1 % 10);

		resBuf[2 + 5] = 'B';
		resBuf[3 + 5] = '0';
		resBuf[4 + 5] = '0' + (width2 / 100);
		resBuf[5 + 5] = '0' + (width2 % 100) / 10;
		resBuf[6 + 5] = '0' + (width2 % 10);

		resBuf[2 + 10] = 'C';
		resBuf[3 + 10] = '0';
		resBuf[4 + 10] = '0' + (width3 / 100);
		resBuf[5 + 10] = '0' + (width3 % 100) / 10;
		resBuf[6 + 10] = '0' + (width3 % 10);


		resBuf[17] = '#';

		m_comLight->write(resBuf, LEN);
		WaitForWriteAndClose();
	}
}